package com.zzyl.controller.security;

import com.zzyl.base.PageBean;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "角色分页")
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageBean<RoleVo>> pageQuery(@PathVariable("pageNum") Integer pageNum,
                                                      @PathVariable("pageSize") Integer pageSize,
                                                      @RequestBody RoleDto dto) {
        log.info("角色分页:{} {} {}", pageNum, pageSize, dto);
        PageBean<RoleVo> pageBean = roleService.pageQuery(pageNum, pageSize, dto);
        return ResponseResult.success(pageBean);
    }
    @ApiOperation("角色添加")
    @PutMapping
    public ResponseResult<Void> create(@RequestBody RoleDto dto) {
        log.info("角色添加:{}", dto);
        roleService.create(dto);
        return ResponseResult.success();
    }

    @ApiOperation("根据角色查询选中的资源数据")
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<List<Long>> findCheckedResources(@PathVariable("roleId") Long roleId) {
        log.info("根据角色查询选中的资源数据:{}", roleId);
        List<Long> checkedResources = roleService.findCheckedResources(roleId);
        return ResponseResult.success(checkedResources);
    }

    @ApiOperation(value = "角色修改")
    @PatchMapping
    public ResponseResult<Void> updateRole(@RequestBody RoleDto dto) {
        log.info("角色修改:{}", dto);
        roleService.updateRole(dto);
        return ResponseResult.success();
    }
    @ApiOperation("删除角色")
    @DeleteMapping("/{roleIds}")
    public ResponseResult<Void> delete(@PathVariable("roleIds") List<Long> roleIds) {
        log.info("删除角色:{}", roleIds);
        roleService.delete(roleIds);
        return ResponseResult.success();
    }

    @ApiOperation("查询所有角色")
    @PostMapping("/init-roles")
    public ResponseResult<List<RoleVo>> initRoles() {
        log.info("查询所有角色");
        List<RoleVo> roleVos = roleService.initRoles();
        return ResponseResult.success(roleVos);
    }
}
